<template>
  <div class="container">
    <div class="location">
      <!-- 位置相关 -->
      <div class="city" @click="cityClick">{{ currentCity.cityName }}</div>
      <div class="my-position" @click="chosenPosition">
        <span class="text">我的位置</span>
        <img class="icon" src="../../../assets/img/home/icon_location.png" alt="">
      </div>

    </div>
    <!-- 日期范围 -->
    <div class="section date-range bottom-gray-line" @click="changeDate">
      <div class="start">
        <div class="date">
          <span class="tip">入住</span>
          <span class="time">{{ formatStartDate }}</span>
        </div>

      </div>
      <div class="stay">
        共住{{ diffDate }}晚
      </div>
      <div class="end">
        <div class="date">
          <span class="tip">离店</span>
          <span class="time">{{ formatEndDate }}</span>
        </div>
      </div>
    </div>
    <!-- 时间选择组件 -->
    <van-calendar v-model:show="dateShow" type="range" @confirm="onConfirm" color="#ff9854" :round="false"
      :formatter="formatter" confirm-text="确定入住时间" confirm-disabled-text="请选择离店时间" />

    <!-- 价格/人数选择 -->
    <div class="section price-counter bottom-gray-line">
      <div class="start">价格不限</div>
      <div class="right">人数不限</div>
    </div>

    <!-- 关键字 -->
    <div class="section keyword bottom-gray-line">
      <van-field v-model="keyword" placeholder="关键字/位置/民宿名" left-icon="search" :clearable="true" />

    </div>

    <!-- 热门建议 -->
    <div class="section hot-suggest">
      <template v-for="(item, index) in hotSuggestList" :key="index">
        <div class="item" :style="{ color: item.tagText.color, background: item.tagText.background.color }"
          @click="suggestClick(item.tagText.text)">
          {{ item.tagText.text }}
        </div>
      </template>
    </div>

    <!-- 搜索按钮 -->
    <div class="section search-btn">
      <div class="btn" @click="searchBtnClick">开始搜索</div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router';
import { formatMonthDay, getDiffDate } from '@/utils/format_date'
import { storeToRefs } from 'pinia';
import useHomeStore from '@/stores/modules/home';
import useCityStore from '@/stores/modules/city';
import useMainStore from '@/stores/modules/main';
import { computed } from '@vue/reactivity';
const router = useRouter()

const chosenPosition = () => {
  //获取位置
  navigator.geolocation.getCurrentPosition((res) => {
    console.log(res, '获取位置成功')
  }, err => {
    console.log(err, '请求失败')
  }, {
    enableHighAccuracy: true,
    timeout: 0,
    maximumAge: 0
  })
}
const cityClick = () => {
  router.push('/city')
}

// 获取当前城市
const cityStore = useCityStore()
const { currentCity } = storeToRefs(cityStore)

// 日期范围的处理
// const nowDate = new Date()
// const newDate = new Date()
// newDate.setDate(nowDate.getDate() + 1) //向后推迟一天
const mainStore = useMainStore()
// notes 首先将pinia的数据拿到赋值为响应式的
const { startDate, endDate } = storeToRefs(mainStore)
/*
  这里先是给开始时间和结束时间加工成10月xx日的结构,使用computed用来加工成响应式，如果
  startDate,endDate某一个进行修改了，就会重新渲染，然后当你选择时间的时候，直接将
  pinia的时间修改 computed会自动改变
*/
const formatStartDate = computed(() => formatMonthDay(startDate.value))
const formatEndDate = computed(() => formatMonthDay(endDate.value))
const dateShow = ref(false)
// 计算相差时间
const diffDate = ref(getDiffDate(startDate.value, endDate.value))
// 选择完日期过后的时间
const onConfirm = (date) => {
  const selectStartDate = date[0]
  const selectEndDate = date[1]
  mainStore.startDate = selectStartDate
  mainStore.endDate = selectEndDate
  dateShow.value = false
  diffDate.value = getDiffDate(selectStartDate, selectEndDate)
}
// 选择时间的方法
const changeDate = () => {
  dateShow.value = true
}
// 对日历上每一格的内容进行格式化
const formatter = (day) => {
  if (day.type === 'start') {
    day.bottomInfo = '入住';
  } else if (day.type === 'end') {
    day.bottomInfo = '离店';
  }
  return day;
}
// 关键字输入框
const keyword = ref('')

// 热门推荐
const homeStore = useHomeStore()
const { hotSuggestList } = storeToRefs(homeStore)
const suggestClick = (item) => {
  keyword.value = item
}
// 搜索按钮方法
const searchBtnClick = () => {
  router.push({
    path: '/search',
    query: {
      keyword: keyword.value,
    }
  })
}
</script>

<style lang="less" scoped>
.container {
  --van-calendar-popup-height: 100%;

  .location {
    display: flex;
    height: 44px;
    align-items: center;
    padding: 0 20px;

    .city {
      flex: 1;
      font-size: 14px;
      color: #333;
    }

    .my-position {
      width: 87px;
      display: flex;

      .text {
        font-size: 14px;
        color: #666
      }

      .icon {
        margin-left: 5px;
        width: 18px;
        height: 18px;
      }
    }


  }

  .section {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    padding: 0 20px;
    color: #999;
    height: 44px;

    .start {
      flex: 1;
      display: flex;
      height: 44px;
      align-items: center;
    }

    .end {
      min-width: 30%;
      padding-left: 20px;
    }

    .date {
      display: flex;
      flex-direction: column;
      align-items: center;

      .tip {
        font-size: 12px;
        color: #999;
      }

      .time {
        margin-top: 3px;
        color: #333;
        font-size: 15px;
        font-weight: 500;
      }
    }
  }

  .date-range {
    height: 44px;

    .stay {
      flex: 1;
      text-align: center;
      font-size: 12px;
      color: #666;
    }
  }

  .price-counter {
    font-size: 14px;
    display: flex;


    .start {
      border-right: 1px solid var(--line-color);
    }

    .right {
      margin-right: 22px;
    }
  }

  .hot-suggest {
    margin: 10px 0;
    height: auto;

    .item {
      padding: 6px 8px;
      margin: 4px;
      border-radius: 16px;
      font-size: 14px;
      line-height: 1;
    }
  }


  .search-btn {
    .btn {
      width: 342px;
      height: 38px;
      max-height: 50px;
      font-weight: 500;
      font-size: 18px;
      line-height: 38px;
      text-align: center;
      border-radius: 20px;
      color: #fff;
      background-image: var(--theme-linear-gradient);
    }
  }
}
</style>